package com.yytek.nmp.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.nmp.domain.IotDeviceInfo;
import com.yytek.nmp.mapper.IotDeviceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yytek.nmp.mapper.IotDeviceGroupMapper;
import com.yytek.nmp.domain.IotDeviceGroup;
import com.yytek.nmp.service.IIotDeviceGroupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备分组Service业务层处理
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Service
public class IotDeviceGroupServiceImpl implements IIotDeviceGroupService 
{
    @Autowired
    private IotDeviceGroupMapper iotDeviceGroupMapper;
    @Autowired
    private IotDeviceInfoMapper iotDeviceInfoMapper;
    /**
     * 查询设备分组
     * 
     * @param id 设备分组主键
     * @return 设备分组
     */
    @Override
    public IotDeviceGroup selectIotDeviceGroupById(Long id)
    {
        return iotDeviceGroupMapper.selectIotDeviceGroupById(id);
    }

    /**
     * 查询设备分组列表
     * 
     * @param iotDeviceGroup 设备分组
     * @return 设备分组
     */
    @Override
    public List<IotDeviceGroup> selectIotDeviceGroupList(IotDeviceGroup iotDeviceGroup)
    {
        return iotDeviceGroupMapper.selectIotDeviceGroupList(iotDeviceGroup);
    }

    /**
     * 新增设备分组
     * 
     * @param iotDeviceGroup 设备分组
     * @return 结果
     */
    @Override
    public AjaxResult insertIotDeviceGroup(IotDeviceGroup iotDeviceGroup)
    {
        //校验名称是否项目下重复
        List<IotDeviceGroup> cxList = iotDeviceGroupMapper.selectDeviceGroupByProjetIdAndName(iotDeviceGroup);
        if(CollectionUtil.isNotEmpty(cxList)){
            return AjaxResult.error("分组名称重复");
        }else{
            int i = iotDeviceGroupMapper.insertIotDeviceGroup(iotDeviceGroup);
            return i > 0 ? AjaxResult.success() : AjaxResult.error("新增失败");
        }

    }

    /**
     * 修改设备分组
     * 
     * @param iotDeviceGroup 设备分组
     * @return 结果
     */
    @Override
    public AjaxResult updateIotDeviceGroup(IotDeviceGroup iotDeviceGroup)
    {
        //校验名称是否项目下重复
        List<IotDeviceGroup> cxList = iotDeviceGroupMapper.selectDeviceGroupByProjetIdAndNameNoThis(iotDeviceGroup);
        if(CollectionUtil.isNotEmpty(cxList)){
            return AjaxResult.error("修改分组名称重复");
        }else{
            int i = iotDeviceGroupMapper.updateIotDeviceGroup(iotDeviceGroup);
            return i > 0 ? AjaxResult.success() : AjaxResult.error("修改失败");
        }
    }

    /**
     * 批量删除设备分组
     * 
     * @param ids 需要删除的设备分组主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteIotDeviceGroupByIds(Long[] ids)
    {
        //删除设备中分组id关系
        iotDeviceInfoMapper.deleteDeviceGroupId(ids);
        return iotDeviceGroupMapper.deleteIotDeviceGroupByIds(ids);
    }

    /**
     * 删除设备分组信息
     * 
     * @param id 设备分组主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceGroupById(Long id)
    {
        return iotDeviceGroupMapper.deleteIotDeviceGroupById(id);
    }
    /**
     * 查询项目下未被分组的设备
     * @param projectId
     * @return
     */
    @Override
    public List<IotDeviceInfo> noBindDeviceList(String projectId) {
        Long id = Long.valueOf(projectId);
        return iotDeviceInfoMapper.selectNoBindGroupDeviceByProjectId(id);
    }
}
